package com.example.gcmtest1.youtube;

import java.io.File;
import java.util.ArrayList;

import com.example.gcmtest1.R;

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

public class DownloadAdapter extends ArrayAdapter<Object>{

	private String logName = "DownloadAdapter";
	
	private Context context;
	
	public DownloadAdapter(Context context, ArrayList<Object> objects) {
		super(context, R.layout.progress_subview, objects);
		this.context = context;
	}

	@Override	
	public View getView(int position, View convertView, ViewGroup parent) {
		Object obj = this.getItem(position);
		Log.d(logName, "position=" + String.valueOf(position) + ", obj=" + obj.getClass().getName());

	    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    View rowView = inflater.inflate(R.layout.progress_subview, parent, false);

	    TextView lblFileName = (TextView) rowView.findViewById(R.id.lblFileName);
	    TextView lblFileSize = (TextView) rowView.findViewById(R.id.lblFileSize);
	    ProgressBar pgbFileDownload = (ProgressBar) rowView.findViewById(R.id.pgbFileDownload);
	    LinearLayout layoutDownload1 = (LinearLayout) rowView.findViewById(R.id.layoutDownload1);
	    
	    Log.i(logName, "obj.getClass()=" + obj.getClass().getName());
	    
		if (obj instanceof YoutubeSQLRecord) {
			
			YoutubeSQLRecord r = (YoutubeSQLRecord) obj;
			lblFileName.setText(r.getFileLocation());
		} else if (obj instanceof SmartHTTPDownload) {
			SmartHTTPDownload dl = (SmartHTTPDownload) obj;
			//lblFileName.setText(dl.getFileName());
			rowView = dl.getView(convertView, parent);
		}
		
	    return(rowView);
	}
	
	/*
	@Override	
	public View getView(int position, View convertView, ViewGroup parent) {
		Object obj = this.getItem(position);
		Log.d(logName, "position=" + String.valueOf(position) + ", obj=" + obj.getClass().getName());

	    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	    View rowView = inflater.inflate(R.layout.progress_subview, parent, false);

	    TextView lblFileName = (TextView) rowView.findViewById(R.id.lblFileName);
	    TextView lblFileSize = (TextView) rowView.findViewById(R.id.lblFileSize);
	    ProgressBar pgbFileDownload = (ProgressBar) rowView.findViewById(R.id.pgbFileDownload);
	    LinearLayout layoutDownload1 = (LinearLayout) rowView.findViewById(R.id.layoutDownload1);
	    
		if (obj instanceof File) {
			File ff = (File) obj;
			layoutDownload1.removeView(pgbFileDownload);
			lblFileName.setText(ff.getAbsolutePath() + "/" + ff.getName());
			lblFileSize.setText(String.valueOf(ff.length()) + " bytes");
		} else if (obj instanceof SmartHTTPDownload) {
			SmartHTTPDownload dl = (SmartHTTPDownload) obj;
			//lblFileName.setText(dl.getFileName());
			rowView = dl.getView(convertView, parent);
			
		}
		
	    return(rowView);
	}
	*/
}
